package com.briup.jz.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.briup.jz.bean.BaseUser;
import com.briup.jz.bean.BaseUserExample;
import com.briup.jz.bean.extend.BaseUserExtend;
import com.briup.jz.dao.BaseUserMapper;
import com.briup.jz.dao.BaseUserRoleMapper;
import com.briup.jz.dao.extend.BaseUserExtendMapper;
import com.briup.jz.service.IBaseUserService;
import com.briup.jz.utils.CustomerException;

@Service
public class BaseUserServiceImpl implements IBaseUserService {
	@Autowired
	private BaseUserMapper baseUserMapper;
	@Autowired
	private BaseUserExtendMapper baseUserExtendMapper;
	@Autowired
	private BaseUserRoleMapper baseUserRoleMapper;

	@Override
	public void saveOrUpdate(BaseUser baseUser) throws CustomerException {
		if(baseUser.getId()!=null) {
			baseUserMapper.updateByPrimaryKey(baseUser);
		} else {
			// 判断是否有同名的分类，如果有抛出异常
			BaseUserExample example = new BaseUserExample();
			example.createCriteria().andUsernameEqualTo(baseUser.getUsername());
			List<BaseUser> list = baseUserMapper.selectByExample(example);
			if(list.size()>0) {
				throw new CustomerException("同名的分类已存在");
			}
			baseUserMapper.insert(baseUser);
		}
	}

	@Override
	public List<BaseUser> query(String username) {
		BaseUserExample example = new BaseUserExample();
		if(username!=null){
			example.createCriteria().andUsernameLike("%"+username+"%");
		}
		List<BaseUser> list = baseUserMapper.selectByExample(example);
		return list;
	}

	@Override
	public void deleteById(long id) throws CustomerException {
		BaseUser baseUser = baseUserMapper.selectByPrimaryKey(id);
		if(baseUser == null) {
			throw new CustomerException("要删除的分类信息不存在");
		}
		baseUserMapper.deleteByPrimaryKey(id);
	}

	@Override
	public void addUserWithRoleId(BaseUserExtend baseUserExtend) {
			// 判断是否有同名的分类，如果有抛出异常
			BaseUserExample example = new BaseUserExample();
			example.createCriteria().andUsernameEqualTo(baseUserExtend.getUsername());
			List<BaseUser> list = baseUserMapper.selectByExample(example);
			if(list.size()>0) {
				throw new CustomerException("同名的用户已存在");
			}
			baseUserExtendMapper.insertWithRoleId(baseUserExtend);
			/*BaseUserRole baseUserRole=new BaseUserRole();
			baseUserRole.setUserId(baseUserExtend.getId());
			baseUserRole.setRoleId(baseUserExtend.getRoleId());
			baseUserRoleMapper.insert(baseUserRole);*/
			baseUserRoleMapper.insert(baseUserExtend.getBaseUserRole());
	}

	@Override
	public void updateUserfaceByUserId(long id, String userface) {
		baseUserExtendMapper.updateUserfaceByUserId(id, userface);
	}

	@Override
	public List<BaseUserExtend> pagequery(Long page, Long pageSize, String username, Long roleId, String status) {
		List<BaseUserExtend> list=baseUserExtendMapper.pageQuery(page, pageSize, username, roleId, status);
		int limitPage=1;
		if (list.size()%pageSize==0) {
			limitPage=(int) (list.size()/pageSize);
		}else {
			limitPage=(int) (list.size()/pageSize)+1;
		}
		if (page>limitPage) {
			for (int i = 1; i >0; i++) {
				page=page-1;
				if (page==limitPage){
					break;
				}
			}
		}
		System.out.println("dsafdsf"+limitPage+"sadsad"+(limitPage-1)*5);
		//从第几条数据开始
        //int firstIndex = (Integer.parseInt(page)-1) * Integer.parseInt(pageSize);
		int firstIndex = (int) ((page-1)*pageSize);
        //到第几条数据结束
        //int lastIndex = Integer.parseInt(page) * Integer.parseInt(pageSize);
		int lastIndex = (int) (page * pageSize);
		if (page==(list.size()/pageSize)+1) {
			lastIndex = list.size();
		}
        return list.subList(firstIndex, lastIndex); //直接在list中截取
	}

	@Override
	public List<BaseUserExtend> selectRoleByUserId(long id) {
		return baseUserExtendMapper.selectRoleByUserId(id);
	}

}
